const { ccclass, property } = cc._decorator;
declare let DBManager,dbh;
import BaseView from "../../component/BaseView";
import HzMiniSelectIconList from "./HzMiniSelectIconList";
import Utils = require("../../utils/Utils");
import DrawOne from "../DrawOne";
import UIHelper = require("../../component/UIHelper");
@ccclass
export default class WashCatView extends BaseView {
    @property(cc.Node)
    bg1: cc.Node = null;
    @property(cc.Node)
    bg2: cc.Node = null;
    //触摸区域
    @property(cc.Node)
    bg3: cc.Node = null;

    @property(HzMiniSelectIconList)
    selectList: HzMiniSelectIconList = null;
    //指引小手
    @property(cc.Node)
    guideNodeList: cc.Node[] = [];

    @property(cc.Animation)
    progressBarAni: cc.Animation = null;
    //恭喜获得
    @property(cc.Node)
    endNode: cc.Node = null;

    //开始动画spine
    @property(sp.Skeleton)
    startSpine: sp.Skeleton = null;
    //修复
    @property(sp.Skeleton)
    repairSpine: sp.Skeleton = null;
    //洗澡
    @property(sp.Skeleton)
    washSpine: sp.Skeleton = null;

    //抹泡沫
    @property(DrawOne)
    forthDraw: DrawOne = null;
    //切换猫
    @property(cc.Animation)
    changeCat: cc.Animation = null;
    //衣服
    @property(cc.Node)
    catClothe: cc.Node = null;
    //泡沫消失
    @property(cc.Animation)
    paomoAni: cc.Animation = null;

    //浴缸
    @property(cc.Node)
    yugangNode_Q: cc.Node = null;
    @property(cc.Node)
    yugangNode_H: cc.Node = null;
    //垫子
    @property(cc.Node)
    dianziNode: cc.Node = null;
    //脏猫
    @property(cc.Node)
    dirtyCat: cc.Node = null;

    _point = 1;
    _isPlay = false;
    _isDone = false;
    _Step = ["mianqian","gouzi","shabu2","shuazi","pentou","chuifeng"];//关卡步骤图标拼接
    _StepNum = 6;

    _startMo = false;//是否开始抹泡沫
    
    onLoad() {
        super.onLoad();
        this.bg3.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.bg3.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.bg3.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
        Utils.audioManager.stopBGM();
    };

    onClose(){
        Utils.audioManager.playDefaultBGM();
    };

    start(){
        super.start();
        this.selectList.showList(this._Step, this._StepNum, 1);
        //开始动画
        this._isPlay = true;
        this.startSpine.setCompleteListener(()=>{
            this._isPlay = false;
            if(this.startSpine){
                this.startSpine.node.active = false;
                this.bg1.active = false;
            }
            if(this.repairSpine){
                this.repairSpine.node.active = true;
                this.bg2.active = true;
                this.updatePoint();
            }
        });
        this.startSpine.setAnimation(0,"animation",false);
        Utils.audioManager.playSound("huazhuang/cat/yanjingjiao");
    };

    onTouchStart(event) {
        if(this._point != 4)
            return;
        if (this._isPlay)
            return;
        if (this._isDone)
            return;
        this.forthDraw._touchStart(event,cc.v2(0,0));
    };

    onTouchMove(event) {
        if(this._point != 4)
            return;
        if (this._isPlay)
            return;
        if (this._isDone)
            return;
        this.forthDraw._touchMove(event);
        if(!this._startMo){
            this._startMo = true;
            this.washSpine.setAnimation(0, "01", false);
            Utils.audioManager.playSound("huazhuang/cat/tupaopao");
        }
    };

    onTouchEnd(event) {
        if(this._point != 4)
            return;
        if (this._isPlay)
            return;
        if (this._isDone)
            return;
        this.forthDraw._touchEnd(()=>{
            this.washSpine.setAnimation(0, "04", false);
            this.forthDraw.node.parent.active = false;
            this._point++;
            this.selectList.showList(this._Step, this._StepNum, this._point);
            this.updatePoint();
        });
    };

    updatePoint(){
        if(this._point > 6){//结束
            //@ts-ignore
            this.scheduleOnce(() => {
                //@ts-ignore
                if (!cc.isValid(this.node)) {
                    return;
                }
                this.catClothe.active = true;
                this.selectList.node.active = false;
                //@ts-ignore
                this.scheduleOnce(() => {
                    //@ts-ignore
                    if (!cc.isValid(this.node)) {
                        return;
                    }
                    this.endNode.active = true;
                    Utils.audioManager.playSound("huazhuang/cat/cat_done");
                }, 1);
            }, 1);
            return;
        }
        if(this._point != 4){
            let guide = this.guideNodeList[this._point-1];
            if(guide){
                guide.active = true;
            }
        }
        this.repairSpine.node.active = this._point <= 3;
        this.washSpine.node.active = this._point > 3;
        this.dirtyCat.active = this.washSpine.node.active;
        this.bg2.active = this.repairSpine.node.active;
        this.bg3.active = this.washSpine.node.active;
        this.forthDraw.node.parent.active = this._point == 4;
        this.yugangNode_Q.active = this._point == 4 || this._point == 5;
        this.yugangNode_H.active = this.yugangNode_Q.active;
        this.dianziNode.active = this._point == 6;

        if(this._point == 4){
            this._isPlay = true;
            this.washSpine.setCompleteListener(() => {
                this._isPlay = false;
            });
            this.washSpine.setAnimation(0, "00", false);
            Utils.audioManager.playSound("huazhuang/cat/muyulu");
        }
        
    };

    //点击触发步骤动画
    onClickPlay(event,index){
        let _index = parseInt(index);
        if(this._isPlay)
            return;
        let guide = this.guideNodeList[_index-1];
        if(guide){
            guide.active = false;
        }
        if(this._point != 4){//刷子 手动刷
            if(this.repairSpine.node.active){
                this.repairSpine.setCompleteListener(() => {
                    this._point++;
                    this.selectList.showList(this._Step, this._StepNum, this._point);
                    this._isPlay = false;
                    this.updatePoint();
                });
                this.repairSpine.setAnimation(0, "0" +index, false);
                if(this._point == 1){
                    Utils.audioManager.playSound("huazhuang/cat/mianqian");
                }else if(this._point == 2){
                    Utils.audioManager.playSound("huazhuang/cat/chongzi");
                }else if(this._point == 2){
                    Utils.audioManager.playSound("huazhuang/cat/shabu");
                }
            }else{
                this.washSpine.setCompleteListener(() => {
                    this._point++;
                    this.selectList.showList(this._Step, this._StepNum, this._point);
                    this._isPlay = false;
                    this.updatePoint();
                });
                let s_index = this._point - 3;
                this.washSpine.setAnimation(0, "0" + s_index, false);
                this.progressBarAni.play();
                if(this._point == 5){//淋浴
                    this.paomoAni.play();
                    Utils.audioManager.playSound("huazhuang/cat/linyu");
                    //@ts-ignore
                    this.scheduleOnce(()=>{
                        //@ts-ignore
                        if (!cc.isValid(this.node)){
                            return;
                        }
                        Utils.audioManager.playSound("huazhuang/cat/didashui");
                    }, 4.5);
                }
                if(this._point == 6){//吹风机
                    this.changeCat.play();
                    Utils.audioManager.playSound("huazhuang/cat/chuifengji");
                }
            }
            
            this._isPlay = true;
        }
    };

    onClickEnd(){
        let next = this.params.next && this.params.next.shift();
        if (next) {
            UIHelper.openUI(next, {
                next: this.params.next
            })
            this.node.destroy();
        }
    };
}
